package com.server;

import com.serializetool.Request;
import com.serializetool.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Constructor;

public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelRegistered();
        System.out.println("通道已注册");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception{
        ctx.fireChannelActive();
        System.out.println("通道已激活,连接ip为"+ctx.channel().remoteAddress());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx,Object msg) throws Exception{
//        ByteBuf buf = (ByteBuf)msg;
//        int len = buf.readableBytes();
//        byte[] arr = new byte[len];
//
//        ChannelPipeline pipeline = ctx.pipeline();
//        System.out.println("客户端发送信息是："+buf.toString(CharsetUtil.UTF_8));
          Request request = (Request)msg;
          Response response = new TaskHandler().handle((Request) msg);
          ctx.writeAndFlush(response);
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("完成一次通话");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable ex){
        System.out.println("异常");
        ex.printStackTrace();
        ctx.close();
    }
}
